"use strict";
/* Controllers */
app.controller("yarnApplicationListController", ['$scope', '$location', '$rootScope', '$routeParams', 'yarnApplicationListService', function ($scope, $location, $rootScope, $routeParams, yarnApplicationListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listYarnApplication = function () {
        var vm = $scope.vm;
        var params = $scope.vm.p;
        $('#list_table').bootstrapTable('showLoading');
        $("#refreshTable").addClass('disabled');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
        };
        console.log(params);
        yarnApplicationListService.listYarnApplication(params, procFn, errorFn);
    };

    $scope.confirmDel = function (id, index) {
        vm.toDelId = id;
        vm.toDelIndex = index;
        $('#myModal').modal('show')
    };

    $scope.ok = function () {
        var vm = $scope.vm;
        console.log("toDelId" + vm.toDelId);
        console.log("toDelIndex" + vm.toDelIndex);
        var params = {};
        params.appId = vm.toDelId;
        var procFn = function (response) {
            if (response.success && response.data) {
                $('#list_table').bootstrapTable('remove', {
                    field: 'id',
                    values: [vm.toDelId]
                });
                alert("kill成功")
            } else {
                alert(response.msg);
            }
        };
        yarnApplicationListService.killYarnApplication(params, procFn, errorFn);
        $('#myModal').modal('hide')
    };

    $("#refreshTable").click(function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listYarnApplication()
    });

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [100, 300, 500, 100], pageSize: 100
        });
        //$scope.listYarnApplication();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('yarnApplicationListService', ['ajaxService', function (ajaxService) {

    // this.listYarnApplication4LastTime = function (params, successFunction, errorFunction) {
    //     ajaxService.AjaxPost(params, "/star/api/basic-service/v1/yarn_application/listYarnApplication4LastTime.do", successFunction, errorFunction);
    // };

    this.listYarnApplication = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/yarn_application/listYarnApplication.do", successFunction, errorFunction);
    };

    this.killYarnApplication = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/yarn_application/killYarnApplication.do", successFunction, errorFunction);
    };
}]);

function yarnAppOptFormat(value, row, index) {
    var optCont = '<div style="width:8rem"><a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel("' + row.id + '",' + index + ') >KILL</a>\n'
    var optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="' + row.trackingUrl + '" target="_blank">详情</a></div>'
    return [
        optCont,
        optCont2
    ].join('');
}

function timeFormat(value, row, index) {
    if (parseInt(value) <= 0) {
        return '-'
    }
    return formatDate(value)
}

function useTimeFormat(value, row, index) {
    var secondVal = value / 1000
    if (secondVal < 1) {
        return '-'
    }
    if (secondVal < 60) {
        return secondVal.toFixed(0) + "s"
    } else if (secondVal >= 60 && secondVal < 3600) {
        return (secondVal / 60).toFixed(2) + "min"
    } else if (secondVal >= 3600) {
        return (secondVal / 60 / 60).toFixed(2) + "h"
    }
}

// function getLocalTime(nS) {
//     return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, ' ');
// }

function formatDate(obj) {
    var time = new Date(parseInt(obj));
    var y = time.getFullYear();  //年
    var m = time.getMonth() + 1;  //月
    if (m < 10) { m = '0' + m }
    var d = time.getDate();  //日
    if (d < 10) { d = '0' + d }
    var h = time.getHours();  //时
    if (h < 10) { h = '0' + h }
    var mm = time.getMinutes();  //分
    if (mm < 10) { mm = '0' + mm }
    var s = time.getSeconds();  //秒
    if (s < 10) { s = '0' + s }
    var timeStr = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
    return timeStr;
}

function storeFormat(value, row, index) {
    // return parseInt(value) / 1024.0 + "GB"
    return toThousands(value) + "MB"
}


function progressFormat(value, row, index) {
    var num = new Number(value).toFixed(2)
    var process = '<div class="progress" style="width:15rem">\n<div class="progress-bar" role="progressbar" aria-valuenow="' + num + '" aria-valuemin="10" aria-valuemax="100" style="width: ' + num + '%">\n<span class="sr-only" style="position: inherit;margin:2px">' + num + '%</span>\n</div>\n</div>'
    return process
}

// function applicationIDOptFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     return [
//         '<a href="#/star/order/yarn_application_detail/', row.appID, '" target="_blank">', "<u>KILL</u>" , '</a>'
//     ].join('');
// }
// function trackingUrlFmtFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     return [
//         '<a href="', value, '" target="_blank">', "<u>详情</u>" , '</a>'
//     ].join('');
// }
// function progressFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     var d = parseInt(value);
//     return [
//         d , '%'
//     ].join('');
// }
// function queueFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     var items = value.split('_');
//     return items[2];
// }

// function memoryFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     return [
//         (parseFloat(value)/1024).toFixed(0) , 'GB'
//     ].join('');
// }
// function vcoreFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     return [
//         value , 'core'
//     ].join('');
// }
// function jobNameFmt(value, row, index) {
//     if (value == null) {
//         return "-";
//     }
//     return value.substring(0,80);
// }
